﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.Entities;
using BaHuan.BusinessLayer;
using BaHuan.Common;

namespace BaHuan
{
    public partial class FrmPhanQuyen : Form
    {
        ChucVuMgt chucvuMgt = new ChucVuMgt();
        PhanQuyenMgt phanquyenMgt = new PhanQuyenMgt();
        ChucNangMgt chucnangMgt = new ChucNangMgt();

        public List<PhanQuyen> GetAllPhanQuyen()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = phanquyenMgt.GetAllPhanQuyen();

            List<PhanQuyen> lstPhanQuyen = new List<PhanQuyen>();
            lstPhanQuyen = (List<PhanQuyen>)resultObj.Result;

            return lstPhanQuyen;
        }

        public List<ChucVu> GetAllChucVu()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chucvuMgt.GetAllChucVu();

            List<ChucVu> lstChucVu = new List<ChucVu>();
            lstChucVu = (List<ChucVu>)resultObj.Result;

            return lstChucVu;
        }

        public List<ChucNang> GetAllChucNang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chucnangMgt.GetAllChucNang();

            List<ChucNang> lstChucNang = new List<ChucNang>();
            lstChucNang = (List<ChucNang>)resultObj.Result;

            return lstChucNang;
        }

        public FrmPhanQuyen()
        {
            InitializeComponent();
        }

        public void RefreshGrid(int idChucVu)
        {
            dgvChucNang.AutoGenerateColumns = false;
            dgvChucNang.DataSource = GetAllChucNang();

            List<PhanQuyen> lst = (from p in GetAllPhanQuyen()
                                   where p.IdChucVu == idChucVu
                                   select p).ToList();

            if (lst.Count > 0)
            {
                foreach (PhanQuyen p in lst)
                {
                    foreach (DataGridViewRow row in this.dgvChucNang.Rows)
                    {
                        if (Convert.ToInt32(row.Cells[0].Value) == p.IdChucNang && p.Quyen == 1)
                            row.Cells[3].Value = 1;
                    }
                }
            }
        }

        private void FrmPhanQuyen_Load(object sender, EventArgs e)
        {
            cboChucVu.DataSource = GetAllChucVu();
            cboChucVu.ValueMember = "Id";
            cboChucVu.DisplayMember = "TenChucVu";
            cboChucVu.SelectedIndex = 0;
            
            int idChucVu = ((ChucVu)cboChucVu.SelectedItem).Id;
            RefreshGrid(idChucVu);
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            ResultObj result = new ResultObj();

            int idChucVu = ((ChucVu)cboChucVu.SelectedItem).Id;

            foreach (DataGridViewRow row in this.dgvChucNang.Rows)
            {                
                int idChucNang = Convert.ToInt32(row.Cells[0].Value);
                int quyen = Convert.ToInt32(row.Cells[3].Value);

                PhanQuyen phanquyen = new PhanQuyen();
                phanquyen = (from p in GetAllPhanQuyen()
                                       where p.IdChucVu == idChucVu && p.IdChucNang == idChucNang
                                       select p).FirstOrDefault();

                if (phanquyen != null)
                {
                    phanquyen.Quyen = quyen;
                    result = phanquyenMgt.UpdatePhanQuyen(phanquyen);
                }
                else
                {
                    phanquyen = new PhanQuyen();
                    phanquyen.IdChucVu = idChucVu;
                    phanquyen.IdChucNang = idChucNang;
                    phanquyen.Quyen = quyen;
                    result = phanquyenMgt.SaveNewPhanQuyen(phanquyen);
                }
                
                if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                {
                    MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
                    break;
                }
                else if (result.ReturnCode < 0)
                {
                    MessageBox.Show("Thất bại");
                    break;
                }
            }

            if (result.ReturnCode > 0)
                    MessageBox.Show(" Lưu thành công", "Thông báo");
        }

        private void dgvChucNang_ColumnHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.ColumnIndex == 3)
            {
                DataGridViewColumn col = dgvChucNang.Columns[e.ColumnIndex];
                if (col.HeaderText == "Chọn hết")
                {
                    foreach (DataGridViewRow row in this.dgvChucNang.Rows)
                    {
                        row.Cells[3].Value = 1;
                    }
                    col.HeaderText = "Bỏ chọn";
                }
                else
                {
                    foreach (DataGridViewRow row in this.dgvChucNang.Rows)
                    {
                        row.Cells[3].Value = 0;
                    }
                    col.HeaderText = "Chọn hết";
                }
            }
        }

        private void cboChucVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            int idChucVu = ((ChucVu)cboChucVu.SelectedItem).Id;
            RefreshGrid(idChucVu);
        }

        private void dgvChucNang_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {
            e.Cancel = true;
        }
    }
}
